package com.wang.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import com.theokanning.openai.edit.EditChoice;
import com.theokanning.openai.edit.EditRequest;
import com.theokanning.openai.edit.EditResult;
import com.wang.Utils.Key;
import com.wang.properties.OpenAi;
import com.wang.service.ApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

/**
 * @Author wang
 * @Description ApiService实现类
 * @Date 2023/2/19 14:35
 */
@Service
@Slf4j
public class ApiServerImpl implements ApiService {
    private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(60);
    private Duration timeout = DEFAULT_TIMEOUT;

    private OpenAiService openAiService;
    @Resource
    private OpenAi openAi;
    private Key key1=new Key();

    public void init() {
        this.openAiService = new OpenAiService(this.key1.getKey(), timeout);
    }

    /**
     * simple interface.<br>
     * Creates a completion for the provided prompt and parameters
     */
    @Override
    public String createCompletion(String message) {
        if (StrUtil.isEmptyIfStr(this.key1)|| ObjectUtil.isNull(this.openAiService)){
            return "请先设置key！";
        }
        log.info("me--->"+message);
        CompletionRequest completionRequest = this.buildCompletionRequest(message);
        return this.createCompletion(completionRequest);
    }

    @Override
    public String setKey(String key) {
        key1.setKey(key);
        //初始化,设置key
        init();
        return "操作成功";
    }

    /**
     * Creates a completion for the provided prompt and parameters
     */
    protected String createCompletion(CompletionRequest completionRequest) {
        StringBuilder response = new StringBuilder();
        CompletionResult completion = this.openAiService.createCompletion(completionRequest);
        List<CompletionChoice> choices = completion.getChoices();
        choices.forEach(choice -> response.append(choice.getText()));
        //去掉响应文本开头的\n\n
        String res = StrUtil.replaceFirst(response.toString(), "\n\n", "");
        log.info("chatGPT--->"+"正在效应");
        return res;
    }

    /**
     * Creates a new edit for the provided input, instruction, and parameters
     */
    public String createEdit(EditRequest editRequest) {
        StringBuilder response = new StringBuilder();
        EditResult edit = this.openAiService.createEdit(editRequest);
        List<EditChoice> choices = edit.getChoices();
        choices.forEach(choice -> response.append(choice.getText()));
        // 去掉响应文本开头的\n\n
        return StrUtil.replaceFirst(response.toString(), "\n\n", "");
    }


    private CompletionRequest buildCompletionRequest(String message) {
        return CompletionRequest.builder().prompt(message)
                .model(this.openAi.getModel())
                .maxTokens(this.openAi.getMax_tokens())
                .build();
    }
}
